/*
 * dev_conf.c
 *
 *  Created on: May 12, 2016
 *      Author: Alan Wang
 */

#include <stdio.h>
#include "dev_conf.h"
#include "../../app/gvar.h"
#include "../../util/log.h"
#include "../../lib/configini/configini.h"
#include "../../util/net_util.h"
#include "../../util/str_util.h"

#define PATH_OF_DEV_CONF "src/business/conf/dev_conf.ini"
#define PATH_OF_PING_IP_LIST "src/business/conf/ping_ip_list.ini"

#define DEV_SECTION "Device"
#define DEV_KEY_WIFI_SSID 		"dev_wifi_ssid"
#define DEV_KEY_WIFI_PWD 		"dev_wifi_pwd"
#define DEV_KEY_WIFI_MAC 		"dev_mac"
#define DEV_KEY_MASTER_ID 		"dev_master_id"
#define DEFAULT_ETHNAME			"eth0"


Config* t_cfg = NULL;
static bool is_read_sus = false;
char DEV_DEFAULT_SSID[20] = "WL-Link_";
char DEV_DEFAULT_PWD[20] = "12345678";

//declare the functions
bool check_conf();
bool save_conf();

void init_dev_config(){
	char mac[18];
	get_mac(mac, DEFAULT_ETHNAME);
	char n_mac[12];
	remove_colon_of_mac(mac, n_mac);
	pstr_copy(DEV_DEFAULT_SSID, 0, "WL-Link_", 0, 8);
	pstr_copy(DEV_DEFAULT_SSID, 8, n_mac, 6, 6);
	Log("init_dev_config()--->>>DEV_DEFAULT_SSID = %s\n", DEV_DEFAULT_SSID);
	get_dev_wifi_conf(&g_dev_conf);
}

bool check_conf(){
	if(!is_read_sus){
		int ret = ConfigReadFile(PATH_OF_DEV_CONF, &t_cfg);
		Log("check_conf()--->>>ret = %d\n", ret);
		if(ret == CONFIG_OK){
			is_read_sus = true;
		}else{
			is_read_sus = false;
		}
	}
	Log("check_conf()--->>>is_read_sus = %d\n", is_read_sus);
	return is_read_sus;
}

bool save_conf(){
	int ret = ConfigPrintToFile(t_cfg, PATH_OF_DEV_CONF);
	return ret == CONFIG_OK ? true : false;
}

int set_dev_master_id(int master_id){
	Log("set_dev_master_id()--->>>master_id = %d\n", master_id);
	if(check_conf()){
		int ret = ConfigAddInt(t_cfg, DEV_SECTION, DEV_KEY_MASTER_ID, master_id);
		if(ret == CONFIG_OK){
			if(save_conf()){
				g_dev_conf.dev_master_id = master_id;
				return 1;
			}else{
				ConfigRemoveKey(t_cfg, DEV_SECTION, DEV_KEY_MASTER_ID);
				return 0;
			}
		}
		return 0;
	}else{
		return 0;
	}
}

int get_dev_master_id(){
	int master_id = 0;
	if(check_conf()){
		ConfigReadInt(t_cfg, DEV_SECTION, DEV_KEY_MASTER_ID, &master_id, 0);
	}
	return master_id;
}

int set_dev_wifi_ssid(char* ssid){
	Log("set_dev_wifi_info()--->>>ssid = %s\n", ssid);
	if(check_conf()){
		int ret = ConfigAddString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_SSID, ssid);
		if(ret == CONFIG_OK){//if add ssid success than add password
			if(save_conf()){
				strcpy(g_dev_conf.dev_wifi_ssid, ssid);
				return 1;
			}else{
				return 0;
			}
		}
		return 0;
	}else{
		return 0;
	}
}

void get_dev_wifi_ssid(char* ssid){
	if(check_conf()){
		ConfigReadString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_SSID, ssid, MAX_SSID_LEN, DEV_DEFAULT_SSID);
	}
}

int set_dev_wifi_pwd(char* password){
	Log("set_dev_wifi_info()--->>>password = %s\n", password);
	if(check_conf()){
		int ret = ConfigAddString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_PWD, password);
		if(ret == CONFIG_OK){//if add ssid success than add password
			if(save_conf()){
				strcpy(g_dev_conf.dev_wifi_pwd, password);
				return 1;
			}else{
				return 0;
			}
		}
		return 0;
	}else{
		return 0;
	}
}

void get_dev_wifi_pwd(char* password){
	if(check_conf()){
		ConfigReadString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_PWD, password, MAX_PWD_LEN, DEV_DEFAULT_PWD);
	}
}


int set_dev_wifi_info(char* ssid, char* password){
	Log("set_dev_wifi_info()--->>>ssid = %s--->>>password = %s\n", ssid, password);
	if(check_conf()){
		int ret = ConfigAddString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_SSID, ssid);
		if(ret == CONFIG_OK){//if add ssid success than add password
			ret = ConfigAddString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_PWD, password);
			if(ret == CONFIG_OK){//if add password success than save them
				if(save_conf()){
					strcpy(g_dev_conf.dev_wifi_ssid, ssid);
					strcpy(g_dev_conf.dev_wifi_pwd, password);
					return 1;
				}else{
					return 0;
				}
			}else{//if add password failed than remove added ssid
				ConfigRemoveKey(t_cfg, DEV_SECTION, DEV_KEY_WIFI_SSID);
			}
		}
		return 0;
	}else{
		return 0;
	}
}


void get_dev_wifi_conf(DevConfig* dev_conf){
	if(check_conf()){
		ConfigReadInt(t_cfg, DEV_SECTION, DEV_KEY_MASTER_ID, &dev_conf->dev_master_id, 0);
		ConfigReadString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_SSID, dev_conf->dev_wifi_ssid, MAX_SSID_LEN, DEV_DEFAULT_SSID);
		ConfigReadString(t_cfg, DEV_SECTION, DEV_KEY_WIFI_PWD, dev_conf->dev_wifi_pwd, MAX_PWD_LEN, DEV_DEFAULT_PWD);
		get_mac(dev_conf->dev_mac, DEFAULT_ETHNAME);
	}
}

int get_ping_ip_list(PingIp* ip_list, int max_count){
	FILE * fp;
	char * line = NULL;
	size_t len = 0;
	ssize_t read;
	int line_count = 0;
	fp = fopen(PATH_OF_PING_IP_LIST, "r");
	if(fp == NULL){
		return -1;
	}
	while ((read = getline(&line, &len, fp)) != -1) {
		//Log("%s", line);
		if(line_count < max_count){
			if(read > 1){//filter the space line
				strcpy(ip_list[line_count].host, line);
				line_count++;
			}
		}else{
			break;
		}

	}
	if(line){
		free(line);
	}
	fclose(fp);
	Log("line_count = %d\n", line_count);
	return line_count;
}

